package com.example.myapplication;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.ContentView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity{
    private List<Fragment> myFragment;
    private String[] titleArr = {"待完成", "待提交", "主动检查"};
    private int[] selectedArr = {R.mipmap.bread, R.mipmap.bread, R.mipmap.bread};
    private int[] unSelectedArr = {R.mipmap.bread_show, R.mipmap.bread_show, R.mipmap.bread_show};
    private final int DEFAULT_POSITION = 0;
    private ViewPager viewpager;
    private TabLayout tabLayout;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        viewpager =  findViewById(R.id.view_pager);
        tabLayout = findViewById(R.id.tab_layout);
        initLayout();
    }

    /**
     * 导航栏布局：https://blog.csdn.net/xiaoshuxgh/article/details/80389570
     */
    private void initLayout() {
        myFragment = new ArrayList<>();
        myFragment.add(new BlankFragment());
        myFragment.add(new BlankFragment2());
        myFragment.add(new BlankFragment3());

        viewpager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return myFragment.get(i);
            }

            @Override
            public int getCount() {
                return myFragment.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titleArr[position];
            }
        });

        tabLayout.setupWithViewPager(viewpager);

        // 注意：这个方法需要放在setupWithViewPager()后面
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tabView = tabLayout.getTabAt(i);
            tabView.setCustomView(getTabView(i));
        }

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置选中图标样式
                View tabView = tab.getCustomView();
                tabView.findViewById(R.id.tabicon).setBackgroundResource(selectedArr[tab.getPosition()]);

                //设置选中字体颜色
                TextView textView = tabView.findViewById(R.id.tabtext);
                textView.setTextColor(getResources().getColor(R.color.colorPrimary));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //设置未选中图标样式
                View tabView = tab.getCustomView();
                tabView.findViewById(R.id.tabicon).setBackgroundResource(unSelectedArr[tab.getPosition()]);

                //设置未选中字体颜色
                TextView textView = tabView.findViewById(R.id.tabtext);
                textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    /**
     * 使用自定义的View布局
     *
     * @param position
     * @return
     */
    private View getTabView(int position) {
        View v = LayoutInflater.from(this).inflate(R.layout.icon_view, null);
        ImageView iv = v.findViewById(R.id.tabicon);
        TextView tv = v.findViewById(R.id.tabtext);
        tv.setText(titleArr[position]);
        //设置默认页面
        if (position == DEFAULT_POSITION) {
            iv.setBackgroundResource(selectedArr[position]);
            tv.setTextColor(v.getResources().getColor(R.color.colorPrimaryDark));
        } else {
            iv.setBackgroundResource(unSelectedArr[position]);
            tv.setTextColor(v.getResources().getColor(R.color.colorPrimary));
        }
        return v;
    }
}

